<template>
	<view class="bg">
		<view class="calendar">
			<uni-calendar class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="change" />
		</view>

		<view class="tongzhi">
			<view class="title">
				紧急通知
			</view>
			<view class="content">
				<view class="" v-if="noticeList">
					<view class="tip" v-for="(item,index) in noticeList">

						{{index+1}}.{{item.content}}
						<view v-show="(item.isReceive==0)&&(item.infoType=='紧急通知(需回复)')">
							<!-- <view class="" v-show="item.infoType=='紧急通知（需回复）'"> -->
							<button v-if="isReceive[index].isReceive != 2" :data-id="item.id" :data-index="index"
								type="default" @click="onReceive"
								:class="isReceive[index].isReceive == 1 ? 'btn_selected' : 'btn_unselected' ">收到</button>
							<button v-else :data-isReceive="item.isReceive" :data-id="item.id" :data-index="index"
								type="default" @click="onReceive" class="btn_unselected"
								style="width:130rpx; font-size: 1rpx">确认中...</button>
							<!-- </view> -->
						</view>

					</view>

				</view>

				<view class="tip" v-else>
					- -今日还未发布新的紧急通知- -
				</view>
			</view>
			<view>

			</view>
		</view>
		<view class="tongzhi">
			<view class="title">
				今日活动
			</view>
			<view class="content">
				<view class="" v-if="isActives">
					<view class="tip" v-for="(item,index) in activesList">
						{{index+1}}.{{item.content}}
					</view>
				</view>
				<view class="tip" v-else>
					- -今日还未发布新活动- -
				</view>
			</view>

		</view>
		<view class="" v-show="showTap">
			<view class="popup">
				<view v-if="isRevise" class="title">
					修改日程
				</view>
				<view v-else class="title">
					新建日程
				</view>
				<view v-if="isRevise" class="xingcheng">
					<view class="part">


						<!-- <view class="input"> -->

						<view class="input">
							<view v-for="(item,index) in info.selected" v-show="item.date==id">
								<input class="input" type="text" id='fresh' v-model="value1" @input="getValue1"
									placeholder="标题1" />
							</view>

						</view>
					</view>
					<!-- <view class=""  v-for="(item,index) in info.selected"   v-show="item.date==id">
						 <input class="input" type="text" id='fresh'  v-model="value1" @input="getValue1"  placeholder="标题1" />
					</view> -->

					<!-- </view> -->
					<view class="part">
						<!-- <view class="" v-for="(item,index) in info.selected"   v-show="item.date==id">
						 <input class="input" type="text" id='fresh'  v-model="value2" @input="getValue2"  placeholder="内容1" />
					</view> -->
						<view class="input">
							<view v-for="(item,index) in info.selected" v-show="item.date==id">
								<input class="input" type="text" id='fresh' v-model="value2" @input="getValue2"
									placeholder="标题1" />
							</view>

						</view>
					</view>
				</view>
				<view v-else class="xingcheng">
					<view class="part">

						<input class="input" type="text" id='fresh' v-model="value1" @input="getValue1"
							placeholder="标题" />
					</view>
					<view class="part">
						<input class="input" type="text" id='fresh' v-model="value2" @input="getValue2"
							placeholder="内容" />
					</view>
				</view>
				<view class="bottoms" v-if="isRevise">
					<view class="bottom" @click="yes()">
						确认修改
					</view>

					<view class="bottom" @click="cancelRevise()">
						取消修改
					</view>
				</view>
				<view class="bottoms" v-else>
					<view class="bottom" @click="yes()">
						确认
					</view>

					<view class="bottom" @click="cancel()">
						取消
					</view>
				</view>

			</view>
		</view>
		<view class="" v-show="showHas">
			<view class="popup">
				<view class="title">
					已有日程
				</view>
				<view class="xingcheng">
					<view class="part">
						<view class="input">
							<view class="title2" v-for="(item,index) in info.selected" v-show="item.date==id">
								{{item.info}}
							</view>
						</view>

					</view>
					<view class="part">
						<view class="input">
							<scroll-view scroll-x class="text2" v-for="(item,index) in info.selected"
								v-show="item.date==id">
								{{item.data}}
							</scroll-view>

						</view>
					</view>
				</view>
				<view class="bottoms">
					<view class="bottom" @click="yes()">
						确认
					</view>

					<view class="bottom" @click="cancel()">
						取消
					</view>
					<view class="bottom" @click="revise()">
						修改
					</view>
					<view class="bottom" @click="deletee()">
						删除
					</view>
				</view>
			</view>

		</view>

	</view>

</template>


<script>
	/**
	 * 获取任意时间
	 */
	function getDate(date, AddDayCount = 0) {
		if (!date) {
			date = new Date()
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/')
		}
		const dd = new Date(date)

		dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

		const y = dd.getFullYear()
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
		console.log(y + m + d + "当前年月日")
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		}
	}
	export default {
		// components: {},
		data() {
			return {
				realId: '',
				isRevise: false,

				isReceive: [],
				isNotice: false,
				isActives: false,
				student: uni.getStorageSync("student"),
				// write: uni.getStorageSync("write"),
				noticeList: '',
				activesList: '',
				id: '',
				showHas: false,
				isWrite: false,
				fulldate: '',
				value1: '',
				value2: '',
				showTap: false,
				showCalendar: false,
				info: {
					lunar: true,
					range: true,
					insert: false,
					// selected: uni.getStorageSync("selected"),
					write: [{
						date: getDate(new Date(), -1).fullDate,
						info: '已打卡',
						data: this.value2,
						id: 'write[0]ididid'
					}],
					selected: [],
				}
			}
		},
		onLoad() {
			this.getActivesList();
			this.getNoticeList();
			this.getItineraryList();
			// console.log("w我执行力行程")
			// this.setAlarm('10','26','消息');
			const dcRichAlert = uni.requireNativePlugin('CRGG-Plugin');
			dcRichAlert.setcalendar({
				title: '我是提醒',
				location: "上海市普陀区",
				allDay: "1",
				description: "测试测试描述",
				startDate: "2022-04-18 s21:29:10",
				endDate: "2022-04-19 15:40:32",
				alarmArray_ios: ["-7.f", "-17.f", "-27.5f"],
				alarmArray_android: [1, 2, 10],

			}, result => {
				if (result.type == "0") {
					//失败 message
				} else {
					//成功
				}
			});
		},
		onReady() {
			this.$nextTick(() => {
				this.showCalendar = true;
			})
			// TODO 模拟请求异步同步数据
			// setTimeout(() => {
			// 	this.info.date = getDate(new Date(),-30).fullDate
			// 	this.info.startDate = getDate(new Date(),-60).fullDate
			// 	this.info.endDate =  getDate(new Date(),30).fullDate;
			// 	this.info.selected.push({
			// 			date: getDate(new Date(),-3).fullDate,
			// 			info: '打卡'
			// 		})
			// 	// this.info.selected.push = [
			// 	// ]
			// }, 2000)
		},
		methods: {
			cancelRevise() {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
					this.isRevise = false;
					this.showHas = true;
					this.showTap = false;
				},1000);
			},
			deletee(e) {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
				this.showHas = false;
				uni.showModal({
					title: "提示",
					content: "确定删除该行程吗？？",
					success: (res) => {
						if (res.confirm) {
							// for (let i = 0; i < this.info.selected.length; i++) {
								// console.log("”selected表里所有的日期："+this.info.selected[i].date+(this.info.selected[i].date  == this.id));
								// console.log("”selected表里所有的日期："+);
								this.info.selected.some(item=>{
									if ((item.date == this.id)) {
										this.realId = item.id;
										return true;
									}
								})
								console.log(this.realId + "this.realId")
							// }
				
							const res7 = this.$myRequest({
								url: '/itinerary/deleteItinerary',
								data: {
									"id": this.realId,
									"no": this.student.no
								},
								method: 'POST',
				
							}).then((res) => {
								console.log(res)
								console.log("s删除成功")
								setTimeout(() => {
									uni.showToast({
										icon: 'success',
										title: '删除成功！'
									})
				
								}, 600)
								uni.reLaunch({
									url: 'index'
								})
				
							})
						} else if (res.cancel) {
				
						}
					}
				})	
				},1000);
			},
			revise(e) {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
					this.value1 ='';
					this.value2 ='';
					this.showTap = true;
					this.showHas = false;
					this.isRevise = true;
					this.info.selected.some( item =>{
						if ((item.date == this.id)) {
							this.value1 =item.info;
							this.value2 = item.data;
								console.log( `${this.value1}==>>this.value1`);
							return true;
						}
					})
				},1000);
				
				// const res4 = this.$myRequest({
				// 	url:'/itinerary/addItinerary',
				// 	data:{
				//    "content":this.value2,
				//    "id": 0,
				//    "no": this.student.no,
				//    "time":  this.fulldate,
				//    "title":this.value1,
				//    },
				// 	method:'POST',
				// 	headers:{
				// 		"content-Type":"application/json"
				// 	}
				// }).then((res)=>{
				// 	console.log("这是增加行程表数据返回"+res);
				// })
			
				


			},
			onReceive(e) {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
					uni.showModal({
						title: "提示",
						content: "确认收到该条紧急通知了吗？",
						success: (res) => {
							if (res.confirm) {
								let index = e.target.dataset.index;
								this.isReceive[index].isReceive = 2;
								setTimeout(() => {
									this.isReceive[index].isReceive = 1;
								}, 1200);
								let id = e.target.dataset.id;
								console.log(this.fulldate + " this.fulldate");
								console.log(e.target.dataset.id + "e.target.dataset.id");
								const res10 = this.$myRequest({
									url: '/receive/reply',
									data: {
										"infoId": id,
										"no": this.student.no,
										"receiveTime": this.fulldate
									},
									method: 'POST',
									headers: {
										"content-Type": "application/json"
									}
								}).then(res => {
									console.log("拿到的数据" + res);
									console.log(this.student.no + this.student.password)
									if (res.data.data) {
										console.log("成功成功成功")
									} else {
										console.log("失败失败")
									}
					
								})
							} else if (res.cancel) {
					
							}
						}
					})
					},1000);
			},
			getActivesList() {
				const res = this.$myRequest({
					url: '/user/activesTodayList',
					data: {
						"no": this.student.no,
						"password": this.student.password
					},
					method: 'POST',
					headers: {
						"content-Type": "application/json"
					}
				}).then(res => {
					console.log("拿到的数据" + res);
					console.log(this.student.no + this.student.password)
					if (res.data.data) {
						this.activesList = res.data.data;
						this.isActives = true;
						console.log("活动列表" + JSON.stringify(this.activesList));
						// console.log(this.activesList[0].content)
					} else {
						console.log("失败失败")
					}


				})

			},
			getNoticeList() {
				const res2 = this.$myRequest({
					url: '/user/noticesList',
					data: {
						"no": this.student.no,
						"password": this.student.password
					},
					method: 'POST',
					headers: {
						"content-Type": "application/json"
					}
				}).then(res => {
					console.log("拿到的数据" + res);
					console.log(this.student.no + this.student.password)
					if (res.data.data) {
						// console.log("成功成功成功")
						this.noticeList = res.data.data;
						this.isNotice = true;
						console.log("通知列表" + JSON.stringify(this.noticeList));
						for (let i = 0; i < this.noticeList.length; i++) {
							this.isReceive.push({
								isReceive: 0
							})
						}

					} else {

						console.log("失败失败")
					}



				})
			},
			getItineraryList() {
				this.info.selected = [];
				this.info.write = [];
				const res3 = this.$myRequest({
					url: '/itinerary/itineraryList',
					data: {
						"no": this.student.no,
					},
					method: 'GET',
					headers: {
						"content-Type": "application/json"
					}
				}).then((res) => {
					console.log("这是显示行程表数据返回" + res);
					this.info.write = res.data.data;
					console.log("这是返回的显示行程表write" + JSON.stringify(this.info.write));

                  	
					 this.info.write.forEach((item)=>{
						this.info.selected.push({
						 date:item.time,
						 info:item.title,
						 data:item.content,
						 id:item.id
						}) 
					 })
					console.log(`${JSON.stringify(this.info.selected)}新的selected表11111)`) 
					// for (let i = 0; i < this.info.write.length; i++) {
					// 	this.info.selected.push({
					// 		date: this.info.write[i].time,
					// 		info: this.info.write[i].title,
					// 		data: this.info.write[i].content,
					// 		id: this.info.write[i].id
					// 	})
					// }

				})
			},

			yes(e) {
	            const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
				if (this.isRevise) {
					// console.log(this.id+"this.id this.id this.id点击修改的日期")
			this.info.selected.some( (item,i) =>{
				if ((this.info.selected[i].date == this.id))
					{
						this.realId = this.info.selected[i].id;
						return true;
					}
			})
					// for (let i = 0; i < this.info.selected.length; i++) {
					// 	if ((this.info.selected[i].date == this.id))
					// 		this.realId = this.info.selected[i].id;
					// }
					console.log(this.realId + " this.realId")
					console.log(this.value1+"this.value1");
					console.log(this.value2+"this.value2");
					const res2 = this.$myRequest({
						url: '/itinerary/addOrEditItinerary',
						data: {
							"content": this.value2,
							"id": this.realId,
							"no": this.student.no,
							"time": this.fulldate,
							"title": this.value1,
						},
						method: 'POST',
						headers: {
							"content-Type": "application/json"
						}
					}).then((res) => {
						console.log("这是修改行程表数据返回" + res);
					})
				
					console.log("修改完了");
					this.isRevise = false;
					setTimeout(() => {
						this.getItineraryList();
						uni.showToast({
							icon: 'success',
							title: '修改成功'
						})
					}, 600);
				
				} else {
					const res2 = this.$myRequest({
						url: '/itinerary/addOrEditItinerary',
						data: {
							"content": this.value2,
							"no": this.student.no,
							"time": this.fulldate,
							"title": this.value1,
						},
						method: 'POST',
						headers: {
							"content-Type": "application/json"
						}
					}).then((res) => {
						console.log("这是增加行程表数据返回" + res);
				
					})
				
					setTimeout(() => {
						this.getItineraryList();
						uni.reLaunch({
							url: 'index'
						});
					}, 100)
				
					console.log("我执行完relaunch了")
				}
				}, 800);
				

				// console.log("日历数据在这里"+JSON.stringify( this.info.selected))
				// console.log(this.info.selected+"this.info.selected")
				// console.log(this.info.selected[this.info.selected.length-1].data+"data内容")
				// console.log(this.info.selected[this.info.selected.length-1].id+"放进selected的id")
				this.showTap = false;
				this.showHas = false;
				// this.value1 = '';
				// this.value2 = '';
				// this.isWrite=true;
				this.isWrite = false;
			},
			getValue1(e) {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
					this.value1 = e.target.value;
					console.log(this.value1 + "value1");
				}, 1000);
			},
			getValue2(e) {
				const self = this;
				clearTimeout(self.tiems);
				self.tiems = setTimeout(() => {
					this.value2 = e.target.value;
					console.log(this.value2 + "value2")
				}, 1000);

			},
			copy() {
				console.log("quxiao");
				uni.switchTab({
					url: '/pages/wode/wode'
				})
			},
			cancel() {
				console.log("quxiao")
				this.showTap = false;
				this.showHas = false;
				this.isWrite = false;
			},

			change(e) {
				console.log('change 返回:', e)
				let value1 = this.value1;
				this.id = e.fulldate;
				console.log(this.id + "datenow");
				console.log(e.fulldate + "e.fulldate ===id");
				// console.log(e.lunar.__ob__.dep.id+"e.extraInfo._ob_.dep.id");
				// for(let x in this.info.write){
				// 	if(x.id==this.id)  isWrite=true;
				// 	console.log(x.id+"x.id")

				// }

				// for(let i=0;i<this.info.write.length;i++){
				// 	if(this.id==this.info.write[i].id) this.isWrite=true;
				// }
				// console.log(this.isWrite+"for循环后的isWrite")
				// console.log(this.info.write[0].id+"this.info.write[0].id")
				this.info.selected.some( item =>{
					if ((item.date == this.id))
						{
							if (this.id == item.date) this.isWrite = true;
							return true;
						}
				});
				if (this.isWrite == false) {
					this.showTap = true;
				} else {
					this.showHas = true;

				}

				// if(!this.isWrite){
				// 		this.showTap=true;
				// }else{
				// uni.showModal({
				// 	title:'已有日程',
				// 	content:value1,
				// 	success:function(res){
				// 		if(res.confirm){
				// 			console.log(this.data.value1+"弹窗value1")
				// 		}else{

				// 		}
				// 	}
				// })

				// }

				console.log('change 返回:', e)
				// 模拟动态打卡
				// if (this.info.selected.length > 5) return
				console.log(e.date + "e.fulldate");
				this.fulldate = e.fulldate;

			},
			confirm(e) {
				console.log('confirm 返回:', e)
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e)
			},
			setAlarm(hour, minutes, message) {
				var that = this;
				var Intent = plus.android.importClass('android.content.Intent');
				var AlarmClock = plus.android.importClass("android.provider.AlarmClock");
				var intent = new Intent(AlarmClock.ACTION_SET_ALARM);

				//闹钟的小时
				intent.putExtra(AlarmClock.EXTRA_HOUR, hour);

				//闹钟的分钟
				intent.putExtra(AlarmClock.EXTRA_MINUTES, minutes);

				//响铃时提示的信息
				intent.putExtra(AlarmClock.EXTRA_MESSAGE, message);

				//对于一次性闹铃，无需指定此 extra
				//一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
				// 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
				//例如[this.Calendar.MONDAY,this.Calendar.TUESDAY,this.Calendar.WEDNESDAY]
				intent.putExtra(AlarmClock.EXTRA_DAYS, this.alarmDayList);

				//用于指定该闹铃触发时是否振动
				intent.putExtra(AlarmClock.EXTRA_VIBRATE, true);

				//如果为true，则调用startActivity()不会进入手机的闹钟设置界面
				intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);

				//清空任务中在其之上的Activity
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

				var main = plus.android.runtimeMainActivity();
				plus.android.importClass("android.app.Activity");

				if (intent.resolveActivity(main.getPackageManager()) != null) {
					main.startActivity(intent);
				}
			}
		}
	}



	////////////////////////////////////////////////////////////////
</script>


<style lang="scss">
	.bg {
		background-color: #FAFAFA;
	}

	.calendar {
		box-shadow: 0 1px 3px rgba(18, 18, 18, 0.2);
	}

	.xingcheng {
		margin: 50rpx;
		margin-top: 20rpx;

		.icon {
			width: 50rpx;
			height: 50rpx;
			background-size: cover;
		}

		.input {
			-webkit-animation: ani3 3s linear infinite;
			overflow: hidden;
			font-size: 35rpx;
			text-align: left;
			height: 110rpx;
			// border: solid 2rpx #64B5F6;

			.title2 {
				color: #3792ce;
				margin-top: 30rpx;
			}

			.text2 {
				margin-top: 30rpx;
				color: #3792ce;
				font-size: 30rpx;
				height: 80rpx;
			}
		}
	}

	.popup {
		position: fixed;
		left: 0;
		bottom: 0rpx;
		width: 100%;
		background-color: #ffffff;
		border-radius: 50rpx;
		box-shadow: 0 -2px 4px rgba(18, 18, 18, 0.1);
		text-align: center;
		z-index: 9999;

		.title {
			margin: 30rpx 0;
		}

		.bottoms {
			display: flex;
			width: 100%;
			padding: 30rpx 0;

			.bottom {
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				font-size: 30rpx;
				color: white;
				background-color: #2196F3;
				border-radius: 5rpx;
				text-align: center;
				margin: 0 30rpx;

				&:last-child {
					background-color: #fff;
					border: 2rpx solid #2196F3;
					color: #2196F3;
				}
			}

		}

		text {
			align-items: center;
			justify-content: center;
			display: flex;
			text-align: center;
			font-size: 36rpx;
			padding: 100rpx 0rpx 100rpx 0rpx;
		}
	}

	.uni-calendar--hook {
		opacity: 0.93 !important;
	}

	@keyframes ani2 {
		0% {
			box-shadow: inset 0 0 0px #319fde;
			box-shadow: inset 0 0 0px #3cc8fa;
			/* #3093bd */
		}

		25% {
			box-shadow: inset 0 0 10px #3cc8fa;

			/* #27acc3 */
		}

		50% {
			box-shadow: inset 0 0 10px #3cc8fa;
			box-shadow: inset 0 0 15px #3cc8fa;
		}

		75% {
			box-shadow: inset 0 0 10px #319fde;
			box-shadow: inset 0 0 10px #3cc8fa;
		}

		100% {
			box-shadow: inset 0 0 0px #319fde;
			box-shadow: inset 0 0 0px #3cc8fa;
		}
	}

	@keyframes ani3 {
		0% {

			box-shadow: inset 0 0 0px #37aef7;
			border-bottom: solid 3rpx #3792ce;
			/* #3093bd */
		}

		25% {
			box-shadow: inset 0 10 0px #37aef7;
			border-bottom: solid 4rpx #3792ce;
			/* #27acc3 */
		}

		50% {
			box-shadow: inset 0 20 0px #37aef7;
			border-bottom: solid 5rpx #3792ce;
		}

		75% {
			box-shadow: inset 0 30 0px #37aef7;
			border-bottom: solid 4rpx #3792ce;
		}

		100% {
			box-shadow: inset 0 0 0px #37aef7;
			border-bottom: solid 3rpx #3792ce;
		}
	}

	.bgimage {
		background-repeat: no-repeat;
		position: fixed;
		z-index: -1;
		background-size: 100%;
		width: 100%;
		min-height: 110%;
		opacity: 0.8;
	}

	.btn_selected {
		// visibility: hidden;
		opacity: 0;
	}

	.btn_unselected {
		margin-right: 20rpx;
		width: 110rpx;
		height: 60rpx;
		line-height: 60rpx;
		/* float: left; */
		background-color: #2196F3 !important;
		color: white !important;
		font-size: 27rpx;
		font-weight: 600;
		margin-top: 10rpx;
	}

	.tongzhi {
		margin: 25rpx;
		padding: 20rpx;
		background-color: #fff;
		box-shadow: 0 1px 3px rgba(18, 18, 18, 0.1);
		border-radius: 5rpx;

	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;

	}

	.calendar-button {
		flex: 1;
		font-weight: bold;
		font-size: 32rpx;

	}

	.content {
		margin-top: 20rpx;
		color: #616161;
		line-height: 1.5;

		.tip {
			margin-top: 10rpx;
			border-bottom: solid 2rpx #2196F3;
			min-height: 200rpx;
		}
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-weight: 600;
		font-size: 32rpx;
		color: #424242;
	}
</style>
